{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "499ea865-b8bc-4153-a737-cc8a52c838c4",
   "metadata": {},
   "source": [
    "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/ourownstory/neural_prophet/blob/main/tutorials/feature-use/collect_predictions.ipynb)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3005d6a8",
   "metadata": {},
   "source": [
    "# Prediction Collection"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "2bb3ac95-e660-4f36-bd03-5bb3ab68aefb",
   "metadata": {},
   "source": [
    "First, let's fit a vanilla model:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "dc722653-e984-4044-bff4-589311e45b4f",
   "metadata": {},
   "outputs": [],
   "source": [
    "if \"google.colab\" in str(get_ipython()):\n",
    "    # uninstall preinstalled packages from Colab to avoid conflicts\n",
    "    !pip uninstall -y torch notebook notebook_shim tensorflow tensorflow-datasets prophet torchaudio torchdata torchtext torchvision\n",
    "    !pip install git+https://github.com/ourownstory/neural_prophet.git # may take a while\n",
    "    #!pip install neuralprophet # much faster, but may not have the latest upgrades/bugfixes\n",
    "\n",
    "import pandas as pd\n",
    "from neuralprophet import NeuralProphet, set_log_level\n",
    "\n",
    "set_log_level(\"ERROR\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "863bf403-5810-491e-922c-9237d81673d7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ds</th>\n",
       "      <th>y</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>141</th>\n",
       "      <td>1960-10-01</td>\n",
       "      <td>461</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>142</th>\n",
       "      <td>1960-11-01</td>\n",
       "      <td>390</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>143</th>\n",
       "      <td>1960-12-01</td>\n",
       "      <td>432</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             ds    y\n",
       "141  1960-10-01  461\n",
       "142  1960-11-01  390\n",
       "143  1960-12-01  432"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_location = \"https://raw.githubusercontent.com/ourownstory/neuralprophet-data/main/datasets/\"\n",
    "df = pd.read_csv(data_location + \"air_passengers.csv\")\n",
    "df.tail(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "db4f3490-2652-4fd5-8b27-bd6d6b88bb67",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = NeuralProphet(n_lags=5, n_forecasts=3)\n",
    "metrics_train = m.fit(df=df, freq=\"MS\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5f0ee247",
   "metadata": {},
   "source": [
    "## Getting the latest forecast df\n",
    "We may get the df of the latest forecast for data analysis."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "42b4cfeb",
   "metadata": {},
   "outputs": [],
   "source": [
    "forecast = m.predict(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "fb18ad51",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ds</th>\n",
       "      <th>y</th>\n",
       "      <th>yhat1</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1960-10-01</td>\n",
       "      <td>461.0</td>\n",
       "      <td>463.035004</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1960-11-01</td>\n",
       "      <td>390.0</td>\n",
       "      <td>410.434906</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1960-12-01</td>\n",
       "      <td>432.0</td>\n",
       "      <td>439.753998</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          ds      y       yhat1\n",
       "0 1960-10-01  461.0  463.035004\n",
       "1 1960-11-01  390.0  410.434906\n",
       "2 1960-12-01  432.0  439.753998"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_fc = m.get_latest_forecast(forecast)\n",
    "df_fc.head(3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1fa31ec2",
   "metadata": {},
   "source": [
    "Number of steps before latests forecast could be included. Here we include 5 steps before latest forecast."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "eae77f28",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ds</th>\n",
       "      <th>y</th>\n",
       "      <th>yhat6</th>\n",
       "      <th>yhat5</th>\n",
       "      <th>yhat4</th>\n",
       "      <th>yhat3</th>\n",
       "      <th>yhat2</th>\n",
       "      <th>yhat1</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1960-05-01</td>\n",
       "      <td>472.0</td>\n",
       "      <td>476.862457</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1960-06-01</td>\n",
       "      <td>535.0</td>\n",
       "      <td>531.84314</td>\n",
       "      <td>527.112732</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1960-07-01</td>\n",
       "      <td>622.0</td>\n",
       "      <td>579.601501</td>\n",
       "      <td>576.99292</td>\n",
       "      <td>590.679077</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          ds      y       yhat6       yhat5       yhat4 yhat3 yhat2 yhat1\n",
       "0 1960-05-01  472.0  476.862457        None        None  None  None  None\n",
       "1 1960-06-01  535.0   531.84314  527.112732        None  None  None  None\n",
       "2 1960-07-01  622.0  579.601501   576.99292  590.679077  None  None  None"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_fc = m.get_latest_forecast(forecast, include_previous_forecasts=5)\n",
    "df_fc.head(3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a820c6e2",
   "metadata": {},
   "source": [
    "Historical data could be included, however be aware that the df could be large."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "efa0985a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ds</th>\n",
       "      <th>y</th>\n",
       "      <th>yhat1</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1949-01-01</td>\n",
       "      <td>112.0</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1949-02-01</td>\n",
       "      <td>118.0</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1949-03-01</td>\n",
       "      <td>132.0</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          ds      y yhat1\n",
       "0 1949-01-01  112.0  None\n",
       "1 1949-02-01  118.0  None\n",
       "2 1949-03-01  132.0  None"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_fc = m.get_latest_forecast(forecast, include_history_data=True)\n",
    "df_fc.head(3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5d621caf-b601-43ca-a64b-a1d7be7453c0",
   "metadata": {},
   "source": [
    "## Collect in-sample predictions"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3d2dc84d-c388-4b59-8fe3-89633e9671fe",
   "metadata": {},
   "source": [
    "## Predictions sorted based on forecast target\n",
    "Calling `predict`, we get a `df_forecast` where each `'yhat<i>'` refers to the `<i>` -step-ahead prediction for **this row's datetime being the target**.\n",
    "Here, `<i>`  refers to the age of the prediction.\n",
    "\n",
    "e.g. `yhat3` is the prediction for this datetime, predicted 3 steps ago, it is \"3 steps old\".\n",
    "\n",
    "Note that the last row `1961-3-01` only has a `yhat3`, which was forecasted at the last location with data `1960-12-01`.\n",
    "Because we lack inputs after that location, we do not have more recent predictions `yhat1` from `1961-2-01` nor  `yhat2` from `1961-1-01`.\n",
    "\n",
    "We also get the individual forecast components, which also refer to their respective contrigution to `yhat<i>`, forecasted `<i>` steps ago.\n",
    "\n",
    "Components without an added number are only time-dependent or based on future regressors, neither are lagged, and thus a single value."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "7a1b8a10-9708-45b9-9f68-1a4be0dd1ee9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ds</th>\n",
       "      <th>y</th>\n",
       "      <th>yhat1</th>\n",
       "      <th>residual1</th>\n",
       "      <th>yhat2</th>\n",
       "      <th>residual2</th>\n",
       "      <th>yhat3</th>\n",
       "      <th>residual3</th>\n",
       "      <th>ar1</th>\n",
       "      <th>ar2</th>\n",
       "      <th>ar3</th>\n",
       "      <th>trend</th>\n",
       "      <th>season_yearly</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>141</th>\n",
       "      <td>1960-10-01</td>\n",
       "      <td>461.0</td>\n",
       "      <td>464.689362</td>\n",
       "      <td>3.689362</td>\n",
       "      <td>467.748444</td>\n",
       "      <td>6.748444</td>\n",
       "      <td>474.838562</td>\n",
       "      <td>13.838562</td>\n",
       "      <td>-217.455673</td>\n",
       "      <td>-214.396606</td>\n",
       "      <td>-207.306473</td>\n",
       "      <td>702.886719</td>\n",
       "      <td>-20.741653</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>142</th>\n",
       "      <td>1960-11-01</td>\n",
       "      <td>390.0</td>\n",
       "      <td>409.214203</td>\n",
       "      <td>19.214203</td>\n",
       "      <td>408.547119</td>\n",
       "      <td>18.547119</td>\n",
       "      <td>417.346649</td>\n",
       "      <td>27.346649</td>\n",
       "      <td>-265.351379</td>\n",
       "      <td>-266.018463</td>\n",
       "      <td>-257.218933</td>\n",
       "      <td>709.864075</td>\n",
       "      <td>-35.298515</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>143</th>\n",
       "      <td>1960-12-01</td>\n",
       "      <td>432.0</td>\n",
       "      <td>424.255768</td>\n",
       "      <td>-7.744232</td>\n",
       "      <td>441.038513</td>\n",
       "      <td>9.038513</td>\n",
       "      <td>440.375763</td>\n",
       "      <td>8.375763</td>\n",
       "      <td>-306.486664</td>\n",
       "      <td>-289.703949</td>\n",
       "      <td>-290.366669</td>\n",
       "      <td>716.616394</td>\n",
       "      <td>14.12606</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            ds      y       yhat1  residual1       yhat2  residual2  \\\n",
       "141 1960-10-01  461.0  464.689362   3.689362  467.748444   6.748444   \n",
       "142 1960-11-01  390.0  409.214203  19.214203  408.547119  18.547119   \n",
       "143 1960-12-01  432.0  424.255768  -7.744232  441.038513   9.038513   \n",
       "\n",
       "          yhat3  residual3         ar1         ar2         ar3       trend  \\\n",
       "141  474.838562  13.838562 -217.455673 -214.396606 -207.306473  702.886719   \n",
       "142  417.346649  27.346649 -265.351379 -266.018463 -257.218933  709.864075   \n",
       "143  440.375763   8.375763 -306.486664 -289.703949 -290.366669  716.616394   \n",
       "\n",
       "    season_yearly  \n",
       "141    -20.741653  \n",
       "142    -35.298515  \n",
       "143      14.12606  "
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.read_csv(data_location + \"air_passengers.csv\")\n",
    "forecast = m.predict(df)\n",
    "forecast.tail(3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0a9c7316-95fd-420e-ac51-1fdf34bd5c51",
   "metadata": {},
   "source": [
    "## Predictions based on forecast start\n",
    "Calling `predict_raw`, we get a `df` where each `'step<i>'` refers to the `<i>`th step-ahead prediction **starting at this row's datetime**.\n",
    "Here, `<i>`  refers to how many steps ahead the prediction is targeted at.\n",
    "\n",
    "e.g. `step0` is the prediction for this datetime. `step1` is the prediction for the next datetime.\n",
    "\n",
    "All the predictions of a particular row were made at the same time: One step before the rows datestamp."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "39e3486e-c86d-4952-a8cd-339817b905fa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ds</th>\n",
       "      <th>step0</th>\n",
       "      <th>step1</th>\n",
       "      <th>step2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>136</th>\n",
       "      <td>1960-10-01</td>\n",
       "      <td>464.689362</td>\n",
       "      <td>408.547119</td>\n",
       "      <td>440.375763</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>137</th>\n",
       "      <td>1960-11-01</td>\n",
       "      <td>409.214203</td>\n",
       "      <td>441.038513</td>\n",
       "      <td>459.443207</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>138</th>\n",
       "      <td>1960-12-01</td>\n",
       "      <td>424.255768</td>\n",
       "      <td>446.244385</td>\n",
       "      <td>455.264343</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            ds       step0       step1       step2\n",
       "136 1960-10-01  464.689362  408.547119  440.375763\n",
       "137 1960-11-01  409.214203  441.038513  459.443207\n",
       "138 1960-12-01  424.255768  446.244385  455.264343"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.read_csv(data_location + \"air_passengers.csv\")\n",
    "forecast = m.predict(df, decompose=False, raw=True)\n",
    "forecast.tail(3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "953b82fd-3e5a-445c-b276-89607281d5f7",
   "metadata": {},
   "source": [
    "Note that the last row contains the last possible forecast, forecasting `1961-1-01`, `1961-2-01` and `1961-3-01` with data available at `1960-12-01`.\n",
    "\n",
    "\n",
    "Setting `decompose=True` will include the individual forecast components, which also refer to their respective contrigution to `step<i>` into the future."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "f86f16de-9535-4573-8c09-8033b7f27ee3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ds</th>\n",
       "      <th>step0</th>\n",
       "      <th>step1</th>\n",
       "      <th>step2</th>\n",
       "      <th>trend0</th>\n",
       "      <th>trend1</th>\n",
       "      <th>trend2</th>\n",
       "      <th>season_yearly0</th>\n",
       "      <th>season_yearly1</th>\n",
       "      <th>season_yearly2</th>\n",
       "      <th>ar0</th>\n",
       "      <th>ar1</th>\n",
       "      <th>ar2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>136</th>\n",
       "      <td>1960-10-01</td>\n",
       "      <td>464.689362</td>\n",
       "      <td>408.547119</td>\n",
       "      <td>440.375763</td>\n",
       "      <td>702.886719</td>\n",
       "      <td>709.864075</td>\n",
       "      <td>716.616394</td>\n",
       "      <td>-20.741653</td>\n",
       "      <td>-35.298515</td>\n",
       "      <td>14.126060</td>\n",
       "      <td>-217.455673</td>\n",
       "      <td>-266.018463</td>\n",
       "      <td>-290.366669</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>137</th>\n",
       "      <td>1960-11-01</td>\n",
       "      <td>409.214203</td>\n",
       "      <td>441.038513</td>\n",
       "      <td>459.443207</td>\n",
       "      <td>709.864075</td>\n",
       "      <td>716.616394</td>\n",
       "      <td>723.593689</td>\n",
       "      <td>-35.298515</td>\n",
       "      <td>14.126060</td>\n",
       "      <td>5.574303</td>\n",
       "      <td>-265.351379</td>\n",
       "      <td>-289.703949</td>\n",
       "      <td>-269.724792</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>138</th>\n",
       "      <td>1960-12-01</td>\n",
       "      <td>424.255768</td>\n",
       "      <td>446.244385</td>\n",
       "      <td>455.264343</td>\n",
       "      <td>716.616394</td>\n",
       "      <td>723.593689</td>\n",
       "      <td>730.571045</td>\n",
       "      <td>14.126060</td>\n",
       "      <td>5.574303</td>\n",
       "      <td>-30.433420</td>\n",
       "      <td>-306.486664</td>\n",
       "      <td>-282.923584</td>\n",
       "      <td>-244.873322</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            ds       step0       step1       step2      trend0      trend1  \\\n",
       "136 1960-10-01  464.689362  408.547119  440.375763  702.886719  709.864075   \n",
       "137 1960-11-01  409.214203  441.038513  459.443207  709.864075  716.616394   \n",
       "138 1960-12-01  424.255768  446.244385  455.264343  716.616394  723.593689   \n",
       "\n",
       "         trend2  season_yearly0  season_yearly1  season_yearly2         ar0  \\\n",
       "136  716.616394      -20.741653      -35.298515       14.126060 -217.455673   \n",
       "137  723.593689      -35.298515       14.126060        5.574303 -265.351379   \n",
       "138  730.571045       14.126060        5.574303      -30.433420 -306.486664   \n",
       "\n",
       "            ar1         ar2  \n",
       "136 -266.018463 -290.366669  \n",
       "137 -289.703949 -269.724792  \n",
       "138 -282.923584 -244.873322  "
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.read_csv(data_location + \"air_passengers.csv\")\n",
    "forecast = m.predict(df, decompose=True, raw=True)\n",
    "forecast.tail(3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c1d12b4a-13a5-4eea-9a41-da1337bc7b99",
   "metadata": {},
   "source": [
    "## Collect out-of-sample predictions\n",
    "This is how you can extend predictions into the unknown future:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "276f79da-e024-4c12-8a68-dc59885a3e92",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_csv(data_location + \"air_passengers.csv\")\n",
    "future = m.make_future_dataframe(df, periods=3)  # periods=m.config_model.n_forecasts, n_historic_predictions=False"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8ab68b04-a6b8-45d1-a9bd-8c4d33d16a40",
   "metadata": {},
   "source": [
    "Now, the forecast dataframe only contains predictions about the yet unobserved future."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "0bdfbbbc-730f-470c-ae86-f1e2d2ac7b56",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ds</th>\n",
       "      <th>y</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1960-11-01</td>\n",
       "      <td>390</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1960-12-01</td>\n",
       "      <td>432</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>1961-01-01</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>1961-02-01</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>1961-03-01</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          ds     y\n",
       "3 1960-11-01   390\n",
       "4 1960-12-01   432\n",
       "5 1961-01-01  None\n",
       "6 1961-02-01  None\n",
       "7 1961-03-01  None"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "future.tail()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6cbb621e-4abd-406b-8c16-23361d9aac15",
   "metadata": {},
   "source": [
    "## Predictions based on forecast target"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "97275377-ca98-417a-9234-897f48cdfd57",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ds</th>\n",
       "      <th>y</th>\n",
       "      <th>yhat1</th>\n",
       "      <th>residual1</th>\n",
       "      <th>yhat2</th>\n",
       "      <th>residual2</th>\n",
       "      <th>yhat3</th>\n",
       "      <th>residual3</th>\n",
       "      <th>ar1</th>\n",
       "      <th>ar2</th>\n",
       "      <th>ar3</th>\n",
       "      <th>trend</th>\n",
       "      <th>season_yearly</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>1961-01-01</td>\n",
       "      <td>NaN</td>\n",
       "      <td>453.751007</td>\n",
       "      <td>NaN</td>\n",
       "      <td>None</td>\n",
       "      <td>NaN</td>\n",
       "      <td>None</td>\n",
       "      <td>NaN</td>\n",
       "      <td>-275.416962</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>723.593689</td>\n",
       "      <td>5.574303</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>1961-02-01</td>\n",
       "      <td>NaN</td>\n",
       "      <td>None</td>\n",
       "      <td>NaN</td>\n",
       "      <td>463.336273</td>\n",
       "      <td>NaN</td>\n",
       "      <td>None</td>\n",
       "      <td>NaN</td>\n",
       "      <td>None</td>\n",
       "      <td>-236.801361</td>\n",
       "      <td>None</td>\n",
       "      <td>730.571045</td>\n",
       "      <td>-30.43342</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>1961-03-01</td>\n",
       "      <td>NaN</td>\n",
       "      <td>None</td>\n",
       "      <td>NaN</td>\n",
       "      <td>None</td>\n",
       "      <td>NaN</td>\n",
       "      <td>522.325989</td>\n",
       "      <td>NaN</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "      <td>-191.955765</td>\n",
       "      <td>736.87323</td>\n",
       "      <td>-22.591486</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          ds   y       yhat1 residual1       yhat2 residual2       yhat3  \\\n",
       "5 1961-01-01 NaN  453.751007       NaN        None       NaN        None   \n",
       "6 1961-02-01 NaN        None       NaN  463.336273       NaN        None   \n",
       "7 1961-03-01 NaN        None       NaN        None       NaN  522.325989   \n",
       "\n",
       "  residual3         ar1         ar2         ar3       trend season_yearly  \n",
       "5       NaN -275.416962        None        None  723.593689      5.574303  \n",
       "6       NaN        None -236.801361        None  730.571045     -30.43342  \n",
       "7       NaN        None        None -191.955765   736.87323    -22.591486  "
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "forecast = m.predict(future)\n",
    "forecast.tail(3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ccd0af24-2aa1-45dc-884f-f27fd71818a2",
   "metadata": {},
   "source": [
    "## Predictions based on forecast start\n",
    "We can also get the forecasts based on the forecast start. here, each `stepX` refers to X steps from datestamp `ds`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "d7fa8af1-c921-4e3c-96e8-d004a1adc751",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ds</th>\n",
       "      <th>step0</th>\n",
       "      <th>step1</th>\n",
       "      <th>step2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1961-01-01</td>\n",
       "      <td>453.751007</td>\n",
       "      <td>463.336273</td>\n",
       "      <td>522.325989</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          ds       step0       step1       step2\n",
       "0 1961-01-01  453.751007  463.336273  522.325989"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "forecast = m.predict(future, raw=True, decompose=False)\n",
    "forecast"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
